import { ESCesiumViewer } from "@czmSrc/ESCesiumViewer";
import { Destroyable, getReactFuncs, ReactParamsType } from "xbsj-base";
import * as Cesium from "cesium";
import { getSceneScaleForScreenPixelSize } from "./getSceneScaleForScreenPixelSize";
import { ESJVector3D } from "earthsdk3";
export class CurrentScale extends Destroyable {
    constructor(
        czmViewer: ESCesiumViewer,
        positionReact: ReactParamsType,
        scaleReact: ReactParamsType,
        axisPixelSize: number
    ) {
        super();
        if (!czmViewer.viewer) return;
        const { scene } = czmViewer.viewer
        const [getPosition, setPosition, positionChanged] = getReactFuncs<ESJVector3D | undefined>(positionReact);
        const [getScale, setScale, ScaleChanged] = getReactFuncs<ESJVector3D | undefined>(scaleReact);
        // 根据相机视角控制坐标轴的缩放
        const centerCartesian = new Cesium.Cartesian3();
        const updateCenter = () => {
            let tempPos = getPosition();
            if (!tempPos) return;
            tempPos = [...tempPos]
            czmViewer.editingHeightOffset && (tempPos[2] -= czmViewer.editingHeightOffset);
            Cesium.Cartesian3.fromDegrees(...tempPos, undefined, centerCartesian);
        }
        updateCenter();
        this.dispose(positionChanged.disposableOn(updateCenter));
        this.dispose(scene.preUpdate.addEventListener(() => {
            if(Cesium.Cartesian3.ZERO.equals(centerCartesian)) return;
            const scale = getSceneScaleForScreenPixelSize(scene, centerCartesian, axisPixelSize);
            if (scale !== undefined) {
                setScale([scale, scale, scale]);
            } else {
                console.warn(`CzmGeoCoordinatesEditor warn: scale: ${scale ?? 'undefined'}`);
            }
        }));
    }
}